﻿using System;
using YIUIBind;
using YIUIFramework;
using UnityEngine;
using System.Collections.Generic;
using Cysharp.Threading.Tasks;
using GameLogic;
using QFramework;
using Random = UnityEngine.Random;

namespace YIUI.Home
{
    /// <summary>
    /// Author  YIUI
    /// Date    2025.8.31
    /// </summary>
    public sealed partial class HomePanel:HomePanelBase
    {
    
        #region 生命周期
        
        protected override void Initialize()
        {
            Debug.Log($"HomePanel Initialize");
        }

        protected override void Start()
        {
            Debug.Log($"HomePanel Start");
        }

        protected override void OnEnable()
        {
            Debug.Log($"HomePanel OnEnable");
        }

        protected override void OnDisable()
        {
            Debug.Log($"HomePanel OnDisable");
        }

        protected override void OnDestroy()
        {
            Debug.Log($"HomePanel OnDestroy");
        }

        protected override async UniTask<bool> OnOpen()
        {
            GameModule.GoPool.Register("Cube",5);
            GameModule.GoPool.Register("Sphere",5);
            
            await UniTask.CompletedTask;
            Debug.Log($"HomePanel OnOpen");
            return true;
        }

        protected override async UniTask<bool> OnOpen(ParamVo param)
        {
            return await base.OnOpen(param);
        }
        
        #endregion

        #region Event开始

        private List<GameObject> goList = new List<GameObject>();
       
        protected override void OnEventSpawnAction()
        {
            var path = "Cube";
            if (Random.Range(0, 1f) > 0.5f)
            {
                path = "Sphere";
            }
            var go = GameModule.GoPool.Spawn(path,2);
            go.transform.Parent((Transform)null)
                .LocalPosition(Random.insideUnitCircle*2)
                .GetComponent<AutoRecycle>().OnRecycle = () =>
                {
                    goList.Remove(go);
                };
            goList.Add(go);
        }
        
        protected override void OnEventRecycleAction()
        {
            if (goList.Count <= 0)
            {
                return;
            }

            var go = goList.GetAndRemoveRandomItem();
            GameModule.GoPool.Recycle(go);
        }
         #endregion Event结束

    }
}